package com.ly.cloud.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.common.mybatisplus.plugins.PageInfo;
import com.ly.cloud.dto.CodeRuleDetailDto;
import com.ly.cloud.dto.CodeRuleTemplateDto;
import com.ly.cloud.service.CodeRuleDetailService;
import com.ly.cloud.service.CodeRuleTemplateService;
import com.ly.cloud.vo.CodeRuleDetailVo;
import com.ly.cloud.vo.CodeRuleOperateDetailVo;
import com.ly.cloud.vo.CodeRuleTemplateVo;
import com.ly.cloud.web.utils.WebResponse;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 编码规则
 * @Author: pengshiqng
 * @Date: Create in 2018/12/6
 */

@RestController
@RequestMapping("/codeRule")
@Api("编码规则Controller")
public class CodeRuleController {

    @Autowired
    private CodeRuleTemplateService codeRuleTemplateService ;

    @Autowired
    private CodeRuleDetailService codeRuleDetailService ;



    //------------------------------编码规则模板-------------------------------------------------------
    /**
     * 条件+分页 查询组
     */
    @RequestMapping(value = "/template/queryPageList", method = RequestMethod.GET)
    public WebResponse<PageInfo<CodeRuleTemplateVo>> queryTemplatePageList(@RequestParam(required = false) String templateName , @RequestParam int pageNum, @RequestParam int pageSize){
        Page<CodeRuleTemplateVo> page = codeRuleTemplateService.selectPage(new Page<CodeRuleTemplateVo>(pageNum, pageSize), templateName);
        return new WebResponse<PageInfo<CodeRuleTemplateVo>>().success(PageInfo.toPageInfo(page));

    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    /*@RequestMapping(value = "/template/deleteById/{id}", method = RequestMethod.POST)
    public WebResponse<Integer> deleteTemplateById(@PathVariable Long id){
        int result = codeRuleTemplateService.deleteById(id) ;
        return new WebResponse<Integer>().success(result);
    }*/

    /**
     * 根据id批量删除
     * @param ids
     * @return
     */
    /*@RequestMapping(value = "/template/deleteByIds", method = RequestMethod.POST)
    public WebResponse<Integer> deleteTemplateByIds(@RequestBody List<Long> ids){
        int result = codeRuleTemplateService.deleteByIds(ids) ;
        return new WebResponse<Integer>().success(result);
    }*/


    /**
     * 新增
     * @param loginUserName
     * @param dto
     * @return
     */
    /*@RequestMapping(value = "/template/insert", method = RequestMethod.POST)
    public WebResponse<Integer> insertTemplate(@RequestHeader(required=false ,value ="loginUserName") String loginUserName,@RequestBody CodeRuleTemplateDto dto){
        int result = codeRuleTemplateService.insert(loginUserName, dto);
        return new WebResponse<Integer>().success(result);
    }*/


    /**
     * 修改
     * @param loginUserName
     * @param dto
     * @return
     */
    @RequestMapping(value = "/template/update", method = RequestMethod.POST)
    public WebResponse<Integer> updateTemplate(@RequestHeader(required=false,value ="loginUserName") String loginUserName,@RequestBody CodeRuleTemplateDto dto){
        int result = codeRuleTemplateService.update(loginUserName, dto);
        return new WebResponse<Integer>().success(result);
    }



    //---------------------------------编码规则详情----------------------------------------------------


    @RequestMapping(value = "/detail/queryList", method = RequestMethod.GET)
    public WebResponse<List<CodeRuleDetailVo>> queryDetailPageList(@RequestParam(required = false) String columnName,@RequestParam(required = false) String bbh){

        List<CodeRuleDetailVo> list = codeRuleDetailService.selectAll(columnName,bbh) ;
        return new WebResponse<List<CodeRuleDetailVo>>().success(list);

    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/deleteById/{id}", method = RequestMethod.POST)
    public WebResponse<Integer> deleteDetailById(@PathVariable("id") Long id){
        int result = codeRuleDetailService.deleteById(id) ;
        return new WebResponse<Integer>().success(result);
    }

    /**
     * 根据id批量删除
     * @param ids
     * @return
     */
    @RequestMapping(value = "/detail/deleteByIds", method = RequestMethod.POST)
    public WebResponse<Integer> deleteDetailByIds(@RequestBody List<Long> ids){
        int result = codeRuleDetailService.deleteByIds(ids) ;
        return new WebResponse<Integer>().success(result);
    }


    /**
     * 点击新增详情的时候弹出框
     * @param templateId
     * @return
     */
    @RequestMapping(value = "/detail/showPop", method = RequestMethod.GET)
    public WebResponse<List<CodeRuleOperateDetailVo>> showPop(@RequestParam String templateId){
        List<CodeRuleOperateDetailVo> list = codeRuleDetailService.showPop(templateId) ;
        return new WebResponse<List<CodeRuleOperateDetailVo>>().success(list);
    }

    /**
     * 新增
     * @param loginUserName
     * @return
     */
    @RequestMapping(value = "/detail/insert", method = RequestMethod.POST)
    public WebResponse<Boolean> insert(@RequestHeader(required=false,value = "loginUserName") String loginUserName, @RequestBody CodeRuleDetailDto dto){
        codeRuleDetailService.insert(loginUserName , dto) ;
        return new WebResponse<Boolean>().success(true);
    }

    /**
     * 修改
     * @param loginUserName
     * @param dto
     * @return
     */
    @RequestMapping(value = "/detail/update", method = RequestMethod.POST)
    public WebResponse<Boolean> update(@RequestHeader(required=false,value = "loginUserName") String loginUserName, @RequestBody CodeRuleDetailDto dto){
        codeRuleDetailService.update(loginUserName, dto);
        return new WebResponse<Boolean>().success(true);

    }

    
    /**
     * 根据表编号查询编码规则，并返回按规则生成的对应值
     * @param bbh 表编号
     * @return
     */
    @RequestMapping(value = "/genRuleCode", method = RequestMethod.GET)
    public WebResponse<String> genRuleCode(@RequestParam String bbh){
        String code = "123456"+bbh;
        return new WebResponse<String>().success(code);
    }



}
